//
//  XQFirstStartCollectionViewController.swift
//  XiaoQiQiaoSwift
//
//  Created by tech-lab on 2016/11/23.
//  Copyright © 2016年 校企桥. All rights reserved.
//

import UIKit

import SnapKit
class FirstStartCell: UICollectionViewCell {
    
     var icon = UIImageView()
   private  var button = UIButton()
    
    override init(frame: CGRect) {
     super.init(frame: frame)
        self.backgroundColor = UIColor.white
        BuildUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func BuildUI(){
        icon = UIImageView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        self.contentView.addSubview(icon)
        button = UIButton.init(frame: CGRect.zero)
        button.setTitle("立即体验", for: .normal)
        self.contentView.addSubview(button)
        button.isHidden = false
        
        button.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView.snp.centerX)
            make.bottom.equalTo(self.contentView.snp.bottom).offset(-50)
            make.width.equalTo(100)
        }
        
        button.addTarget(self, action: #selector(FirstStartCell.beginJoinMain), for: .touchUpInside)
        
    }
    
    func beginJoinMain() {
        let tabbarVc = XQTabBarViewController()
        UserDefaults.standard.set(false, forKey: "Firstluanch")
        UIApplication.shared.keyWindow?.rootViewController = tabbarVc
        
        let anim = CATransition.init()
        anim.type = "pageCurl"
        anim.duration = 1
        UIApplication.shared.keyWindow?.layer.add(anim, forKey: nil)
    }
    
    func setIndexPath(index:NSIndexPath) {
        if index.row == 3 {
            button.isHidden = false
        }else{
            button.isHidden = true
        }
    
    }
    
    
    
}


class XQFirstStartCollectionViewController: UICollectionViewController {

    private let reuseIdentifier = "Cell"

    var pageControl:UIPageControl!
 
    
    init(){
        let layout:UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize.init(width: SCREEN_WIDTH, height: SCREEN_HEIGHT)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Register cell classes
        self.collectionView!.register(FirstStartCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        self.collectionView?.backgroundColor = UIColor.white
        self.collectionView?.isPagingEnabled = true
        self.collectionView?.showsVerticalScrollIndicator = false
        self.collectionView?.showsHorizontalScrollIndicator = false
        self.collectionView?.bounces = false
        
        pageControl = creatPageControl()
        self.collectionView?.addSubview(pageControl)
        
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func creatPageControl() -> UIPageControl {
        pageControl = UIPageControl.init(frame: CGRect.zero)
        return pageControl;
    }
    
    
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: UICollectionViewDataSource

    override func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }


    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! FirstStartCell
        
        let str1 = NSString.init(string: "引导页\(indexPath.row+1)")
        cell.icon.image = UIImage.init(named: str1 as String)
        cell.setIndexPath(index: indexPath as NSIndexPath)
        // Configure the cell
    
        return cell
    }
    
    

    // MARK: UICollectionViewDelegate

    /*
    // Uncomment this method to specify if the specified item should be highlighted during tracking
    override func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment this method to specify if the specified item should be selected
    override func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
        return true
    }
    */

    /*
    // Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
    override func collectionView(_ collectionView: UICollectionView, shouldShowMenuForItemAt indexPath: IndexPath) -> Bool {
        return false
    }

    override func collectionView(_ collectionView: UICollectionView, canPerformAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) -> Bool {
        return false
    }

    override func collectionView(_ collectionView: UICollectionView, performAction action: Selector, forItemAt indexPath: IndexPath, withSender sender: Any?) {
    
    }
    */

}
